package rafael.bot;

import static roshambo.Player.PAPEL;
import static roshambo.Player.PEDRA;
import static roshambo.Player.TESOURA;
import static rafael.bot.VectorUtils.*;

public class PMCModel extends Model {
	private PerceptronMulticlass opponent;
	private int k;

	public PMCModel(int k) {
		this.k = k;
	}

	@Override
	public void init(int moves) {
		opponent = new PerceptronMulticlass(3);
		opponent.init(2 * 3 * k + 1);
	}

	@Override
	public void learn(int[]input, int output, double eta) {
		double[] x = getFormatedInput(input);
		int y = output-1;
		opponent.onlineTrain(x, y, eta);
	}

	@Override
	public int predictMove(int[] input) {
		double result = opponent.predict(getFormatedInput(input));
		return (int)(result + 1);
	}
	
	private double[] formatMove(int move) {
		int rock, paper, scissor;
		rock = move == PEDRA ? 1 : 0;
		paper = move == PAPEL ? 1 : 0;
		scissor = move == TESOURA ? 1 : 0;
		double[] input = { rock, paper, scissor };
		return input;
	}
	private double[] formatMoves(int[] moves) {
		double[] result = new double[0];
		for (int i = 0; i < moves.length; i++) {
			result = concat(result, formatMove(moves[i]));
		}
		return result;
	}

	private double[] getFormatedInput(int[] input) {
		double[] result = formatMoves(input);
		double[] bias = { 1 };
		result = concat(result, bias);
		return result;
	}

}
